﻿#include "computer.h"
#include "ui_computer.h"
#include "state.h"
using namespace std;

computer::computer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::computer)
{
    ui->setupUi(this);
    this->setFixedSize(1000,650);
    this->setWindowTitle("计算机移动");

    start = new QPushButton(this);
    title = new QLabel(this);
    backgame = new QPushButton(this);

    start->setGeometry(700,100,250,75);
    backgame->setGeometry(700,200,250,75);
    title->setGeometry(135,20,250,75);


    title->setFont(QFont("方正楷体",18));
    title->setText("计算机移动");
    start->setFont(QFont("方正楷体",16));
    backgame->setFont(QFont("方正楷体",16));

    start->setText("开始");
    backgame->setText("返回");
    start->setStyleSheet("border: 4px inset rgb(255,255,255);""border-radius:10px;");
    start->setFocusPolicy(Qt::NoFocus);//NoFocus
    backgame->setStyleSheet("border: 4px inset rgb(255,255,255);""border-radius:10px;");
    backgame->setFocusPolicy(Qt::NoFocus);//NoFocus

//    connect(start, &QPushButton::clicked, this, &computer::automove);
    connect(backgame,&QPushButton::clicked,this,&computer::close);
    QImage *backImg = new QImage();
    backImg->load(":/Image/resource/backImg.png");
    QLabel *back = new QLabel(this);
    back->setFixedSize(1000,650);
    back->setPixmap(QPixmap::fromImage(*backImg));
    back->lower();

    blockAudio = new QSound(":/sound/resource/BlockAudio.wav");


}


void computer::layoutbutton(int *p){
    newblock = new QPushButton[9];
    const int a=75;
    const int b=100;
    const int side=100;
    int sqn = 3;
    for(int i=0;i<sqn;i++){
        for(int j=0;j<sqn;j++){
            //将按钮控件添加到当前的窗口中
            newblock[sqn*i+j].setParent(this);
            //(左边缘，上边缘，宽，高)设置大小
            newblock[sqn*i+j].setGeometry(a+side*j,b+side*i,side,side);
            newblock[sqn*i+j].setFont(QFont("HGB8X_CNKI",18));
            newblock[sqn*i+j].setStyleSheet("background-image: url(:/Image/resource/blockImg.jpg);");
            newblock[sqn*i+j].setFocusPolicy(Qt::NoFocus);//NoFocus

            if(!(i == sqn-1 && j == sqn-1))
                newblock[sqn*i+j].setText(QString::number(p[sqn*i+j]));
        }
    }
}



void computer::automove(){
    // 获取当前游戏状态，并转换为solve函数所需的格式
   string current_state = "";
   for (int i = 0; i < 8; ++i) {
       current_state += newblock[i].text().toStdString();
   }
    current_state +="x";
   // 调用solve函数获取解决方案
    solution = solve(current_state);

    QObject::connect(&timer, &QTimer::timeout, this, [this]()
    {
        if (currentIndex < solution.size())
        {
           const std::string& move = solution[currentIndex++];
           // 执行移动操作
           if (move == "up")
           {
               newblock[currentBlockPosition].setText(newblock[currentBlockPosition-3].text());
               newblock[currentBlockPosition-3].setText("");
               currentBlockPosition=currentBlockPosition-3;
               blockAudio->play();
           }
           else if (move == "down")
           {
               newblock[currentBlockPosition].setText(newblock[currentBlockPosition+3].text());
               newblock[currentBlockPosition+3].setText("");
               currentBlockPosition=currentBlockPosition+3;
               blockAudio->play();

           }
           else if (move == "left")
           {
               newblock[currentBlockPosition].setText(newblock[currentBlockPosition-1].text());
               newblock[currentBlockPosition-1].setText("");
               currentBlockPosition=currentBlockPosition-1;
               blockAudio->play();

           }
           else if (move == "right")
           {
               newblock[currentBlockPosition].setText(newblock[currentBlockPosition+1].text());
               newblock[currentBlockPosition+1].setText("");
               currentBlockPosition=currentBlockPosition+1;
               blockAudio->play();

           }
        }
        if (currentIndex == solution.size())
        {
             timer.stop();
             QMessageBox overWindow;
             overWindow.setWindowTitle("计算机玩家");
             overWindow.setText("最少步骤解决方案完成！");
             overWindow.setFixedSize(100,65);
             overWindow.exec();
        }
        });
       timer.start(500);
}

computer::~computer()
{
    delete ui;
}
